﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BaseEntities;
using BussinessLogic;

namespace ATMSystem_G5_HaUI.UC2_Withdraw
{
    public partial class UC2_Withdraw : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        #region Các button xử lý
        protected void btn1_Click(object sender, EventArgs e)
        {
            if (PanelWithdrawMain.Visible == true)
            {
                PanelWithdrawMain.Visible = false;
                if (Withdraw(500000) == true)
                {
                    Session["amount"] = "500000";
                    PanelConfirm.Visible = true;
                }
                else
                    PanelWithdrawOther.Visible = true;
            }
        }

        protected void btn2_Click(object sender, EventArgs e)
        {
            if (PanelWithdrawMain.Visible == true)
            {
                PanelWithdrawMain.Visible = false;
                if (Withdraw(2000000) == true)
                {
                    Session["amount"] = "2000000";
                    PanelConfirm.Visible = true;
                }
                else
                    PanelWithdrawOther.Visible = true;
            }
        }

        protected void btn3_Click(object sender, EventArgs e)
        {
            if (PanelWithdrawMain.Visible == true)
            {
                PanelWithdrawMain.Visible = false;
                if (Withdraw(3000000) == true)
                {
                    Session["amount"] = "3000000";
                    PanelConfirm.Visible = true;
                }
                else
                    PanelWithdrawOther.Visible = true;
            }
        }

        protected void btn4_Click(object sender, EventArgs e)
        {
            if (PanelWithdrawMain.Visible == true)
            {
                PanelWithdrawMain.Visible = false;
                if (Withdraw(1000000) == true)
                {
                    Session["amount"] = "1000000";
                    PanelConfirm.Visible = true;
                }
                else
                    PanelWithdrawOther.Visible = true;
            }
        }

        protected void btn5_Click(object sender, EventArgs e)
        {
            if (PanelWithdrawMain.Visible == true)
            {
                PanelWithdrawMain.Visible = false;
                if (Withdraw(2500000) == true)
                {
                    Session["amount"] = "2500000";
                    PanelConfirm.Visible = true;
                }
                else
                    PanelWithdrawOther.Visible = true;
            }
        }

        protected void btn6_Click(object sender, EventArgs e)
        {
            PanelWithdrawMain.Visible = false;
            PanelWithdrawOther.Visible = true;
        }

        protected void btnEnter_Click(object sender, EventArgs e)
        {
            if (PanelWithdrawOther.Visible == true)
            {
                decimal amount = Convert.ToDecimal(txtAmount.Text);
                Session["amount"] = amount.ToString();
                if (Withdraw(amount) == true)
                {
                    PanelWithdrawOther.Visible = false;
                    PanelConfirm.Visible = true;
                }
            }
            if (PanelPrintReceipt.Visible == true)
            {
                PanelPrintReceipt.Visible = false;
                PanelThanks.Visible = true;
                Response.Redirect("Home.aspx");
            }
            if (PanelConfirm.Visible == true)
            {
                PanelConfirm.Visible = false;
                PrintReceipts();
                PanelPrintReceipt.Visible = true;
            }
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            if (PanelWithdrawOther.Visible == true)
            {
                try
                {
                    string a = txtAmount.Text;
                    a = a.Remove(a.Length - 1);
                    txtAmount.Text = a;
                }
                catch (Exception)
                { }
            }
        }
        #endregion
        #region Các hàm xử lý
        public static int ATMID = 1;
        public Boolean Withdraw(decimal amount)
        {
            string CardNo = "";
            if (Session["CardNo"] != null)
            {
                CardNo = Session["CardNo"].ToString();
            }
            //decimal amount = decimal.Parse(txtAmount.Text);
            CardBLL card = new CardBLL();
            int AccID = card.GetAccountID(CardNo);
            if (ValidateAmount(amount) == true &&
                CheckBalane(AccID, amount) == true &&
               CheckInATM(ATMID, amount) == true)
            {
                UpdateBalace(AccID, amount);
                WriteLog(amount);
                return true;
            }
            return false;
        }

        public Boolean ValidateAmount(decimal amount)
        {
            if (amount > 5000000)
            {
                lbNotify.Text = "Only withdraw 5000000đ for each!";
                txtAmount.Text = "";
                return false;
            }
            if (amount % 50000 != 0)
            {
                lbNotify.Text = "Amount must be multiple of 50000đ";
                txtAmount.Text = "";
                return false;
            }
            return true;
        }

        public Boolean CheckBalane(int AccountID, decimal amount)
        {
            AccountBLL accBLL = new AccountBLL();
            Account acc = new Account();
            acc.Balance = accBLL.GetBalance(AccountID);
            if (amount > acc.Balance)
            {
                lbNotify.Text = "Balance not enought!";
                txtAmount.Text = "";
                return false;
            }
            return true;
        }
        public Boolean CheckInATM(int ATMID, decimal amount)
        {
            MonneyBLL m = new MonneyBLL();
            decimal sum = m.GetMoneyInATM();
            if (amount > sum)
            {
                lbNotify.Text = "Sorry! This ATM not enought money";
                txtAmount.Text = "";
                return false;
            }
            else
            {
                if (CheckTypeOfMoneyInATM(amount) == true)
                {
                    return true;
                }
                else
                    return false;
            }
        }
        public Boolean CheckTypeOfMoneyInATM(decimal amount)
        {
            int loai500 = 0;
            int loai200 = 0;
            int loai100 = 0;
            int loai50 = 0;
            int loai20 = 0;
            int loai10 = 0;
            decimal temp = 0;
            //Check type 500k
            temp = amount % 500000;
            loai500 = Convert.ToInt32((amount - temp) / 500000);
            if (GetQuantity(6) < loai500)
            {
                loai500 = GetQuantity(6);
            }
            amount = amount - loai500 * 500000;
            //Check type 200k
            temp = amount % 200000;
            loai200 = Convert.ToInt32((amount - temp) / 200000);
            if (GetQuantity(5) < loai200)
            {
                loai200 = GetQuantity(5);
            }
            amount = amount - loai200 * 200000;
            //Check type 100k
            temp = amount % 100000;
            loai100 = Convert.ToInt32((amount - temp) / 100000);
            if (GetQuantity(4) < loai100)
            {
                loai100 = GetQuantity(4);
            }
            amount = amount - loai100 * 100000;
            //Check type 50k
            temp = amount % 50000;
            loai50 = Convert.ToInt32((amount - temp) / 50000);
            if (GetQuantity(3) < loai50)
            {
                loai50 = GetQuantity(3);
            }
            amount = amount - loai50 * 50000;
            //Check type 20k
            temp = amount % 20000;
            loai50 = Convert.ToInt32((amount - temp) / 20000);
            if (GetQuantity(3) < loai20)
            {
                loai50 = GetQuantity(3);
            }
            amount = amount - loai20 * 20000;
            //Check type 10k
            temp = amount % 10000;
            loai10 = Convert.ToInt32((amount - temp) / 10000);
            if (GetQuantity(1) < loai10)
            {
                loai50 = GetQuantity(1);
            }
            amount = amount - loai10 * 10000;

            if (amount > 0)
            {
                lbNotify.Text = "Sorry! This ATM not enought money";
                txtAmount.Text = "";
                return false;
            }
            else
            {
                UpdateQuantityInATM(6, loai500);
                UpdateQuantityInATM(5, loai200);
                UpdateQuantityInATM(4, loai100);
                UpdateQuantityInATM(3, loai50);
                UpdateQuantityInATM(2, loai20);
                UpdateQuantityInATM(1, loai10);
                return true;
            }
        }

        public int GetQuantity(int IDMoney)
        {
            StockBLL stbll = new StockBLL();
            Stock st = new Stock();
            st.Quantity = stbll.GetQuantity(ATMID, IDMoney);
            return st.Quantity;
        }

        public void UpdateQuantityInATM(int IDMoney, int Quantity)
        {
            StockBLL st = new StockBLL();
            st.UpdateQuantity(ATMID, IDMoney, Quantity);
        }

        public void UpdateBalace(int AccountID, decimal Amount)
        {
            AccountBLL acc = new AccountBLL();
            acc.UpdateBalance(AccountID, Amount);
        }

        public void WriteLog(decimal amount)
        {
            string CardNo = "";
            if (Session["CardNo"] != null)
            {
                CardNo = Session["CardNo"].ToString();
            }
            LogBLL logBLL = new LogBLL();
            Log log = new Log();
            log.ATMID = 1;
            log.LogTypeID = 1;
            log.CardNo = CardNo;
            log.LogDate = DateTime.Now;
            log.Amount = amount;
            log.Details = "Withdraw";
            logBLL.WriteLog(log);
        }

        public void PrintReceipts()
        {
            string CardNo = "";
            if (Session["CardNo"] != null)
            {
                CardNo = Session["CardNo"].ToString();
            }
            PrintReceiptBLL p = new PrintReceiptBLL();
            DataTable table = p.Print(CardNo);
            foreach (DataRow r in table.Rows)
            {
                Label4.Text = r[0].ToString();
                Label6.Text = r[1].ToString();
                Label8.Text = r[2].ToString();
            }
            Label5.Text = DateTime.Now.ToString();
            Label7.Text = Convert.ToString(Session["amount"]);
        }
        #endregion
        #region Các button số
        protected void num0_Click(object sender, EventArgs e)
        {
            if (PanelWithdrawOther.Visible == true)
            {
                txtAmount.Text += "0";
            }
        }

        protected void num1_Click(object sender, EventArgs e)
        {
            if (PanelWithdrawOther.Visible == true)
            {
                txtAmount.Text += "1";
            }
        }

        protected void num2_Click(object sender, EventArgs e)
        {
            if (PanelWithdrawOther.Visible == true)
            {
                txtAmount.Text += "2";
            }
        }

        protected void num3_Click(object sender, EventArgs e)
        {
            if (PanelWithdrawOther.Visible == true)
            {
                txtAmount.Text += "3";
            }
        }

        protected void num4_Click(object sender, EventArgs e)
        {
            if (PanelWithdrawOther.Visible == true)
            {
                txtAmount.Text += "4";
            }
        }

        protected void num5_Click(object sender, EventArgs e)
        {
            if (PanelWithdrawOther.Visible == true)
            {
                txtAmount.Text += "5";
            }
        }

        protected void num6_Click(object sender, EventArgs e)
        {
            if (PanelWithdrawOther.Visible == true)
            {
                txtAmount.Text += "6";
            }
        }

        protected void num7_Click(object sender, EventArgs e)
        {
            if (PanelWithdrawOther.Visible == true)
            {
                txtAmount.Text += "7";
            }
        }

        protected void num8_Click(object sender, EventArgs e)
        {
            if (PanelWithdrawOther.Visible == true)
            {
                txtAmount.Text += "8";
            }
        }

        protected void num9_Click(object sender, EventArgs e)
        {
            if (PanelWithdrawOther.Visible == true)
            {
                txtAmount.Text += "9";
            }
        }
        #endregion
    }
}